import { selectVersion } from "@/api/my";

/*
 * @Author: fengYM feng25321@163.com
 * @Date: 2023-09-21 16:09:36
 * @LastEditors: fengYM feng25321@163.com
 * @LastEditTime: 2023-09-25 11:34:09
 * @Description:
 */
export default {
  data() {
    return {};
  },
  onLoad: function () {
    let thePages: any = getCurrentPages();
    let thePage = thePages[thePages.length - 1];
    let newArr = thePage.route.split("/");
    let pageName = newArr[newArr.length - 1];
    let arr: string[] = ["goodsDetails"];
    // #ifdef MP-WEIXIN
    if (arr.some((condition) => pageName.includes(condition))) {
      //@ts-ignore
      wx.showShareMenu({
        withShareTicket: true,
        menus: ["shareAppMessage", "shareTimeline"],
      });
    } else {
      //@ts-ignore
      wx.hideShareMenu();
    }
    // #endif
  },
  onShareAppMessage(res: { from: string }) {
    console.log(res, "onShareAppMessage");
    let that: any = this;
    let imageUrl = that.shareUrl || "";
    if (res.from === "button") {
      //这块需要传参，不然链接地址进去获取不到数据
      let path = `/` + that.$scope.route + `?item=` + that.$scope.options.item;
      return {
        title: "",
        path: path,
        imageUrl: imageUrl,
      };
    }
    if (res.from === "menu") {
      console.log(that.$scope.options, "13131");
      console.log(uni.getStorageSync("userInfo"));

      let keys = that.$scope.options;
      keys.invitationCode = uni.getStorageSync("userInfo").invitationCode;
      let path = `/${that.$scope.route}?`;
      for (const key of Object.keys(keys)) {
        path += `${key}=${keys[key]}`;
        // 添加 '&' 分隔符，除非是最后一个属性
        if (key !== Object.keys(keys)[Object.keys(keys).length - 1]) {
          path += "&";
        }
      }
      console.log(path, "path");

      return {
        title: "",
        path: path,
      };
    }
  },
  // 分享到朋友圈
  onShareTimeline() {
    let that: any = this;
    const keys = Object.keys(that.$scope.options)[0];
    return {
      title: "",
      path: `/${that.$scope.route}?${keys}=${that.$scope.options[keys]}`,
    };
  },
};

export const TanPreviewImage = (imgs: any, iIndex: any) => {
  let images = [];
  images = [...imgs];
  uni.previewImage({
    current: iIndex,
    urls: images,
    longPressActions: {
      //长按保存图片到相册
      itemList: ["保存图片"],
      success: (data) => {
        uni.saveImageToPhotosAlbum({
          //保存图片到相册
          filePath: "payUrl",
          success: function () {
            uni.showToast({ icon: "success", title: "保存成功" });
          },
          fail: (err) => {
            uni.showToast({ icon: "none", title: "保存失败，请重新尝试" });
          },
        });
      },
    },
  });
};
export const goBack = () => {
  if (getCurrentPages().length > 1) {
    uni.navigateBack();
  } else {
    uni.switchTab({
      url: "/pages/home/home",
    });
  }
};
export const goPage = (url: string) => {
  uni.navigateTo({
    url,
  });
};

export const getTopHeight = () => {
  let custom = uni.getMenuButtonBoundingClientRect(); // 小程序右上角胶囊信息
  let system: any = uni.getSystemInfoSync(); // 获取设备信息
  let navHeight = system.statusBarHeight + 44;
  let customTop = custom.top;
  let customRight = custom.width;
  return {
    /**
     * 导航栏的高度
     */
    navHeight,
    /**
     * 胶囊按钮与顶部的距离
     */
    customTop,
    /**
     *  胶囊按钮与右侧的距离
     */
    customRight,
  };
};
export function maskBankCard(cardNumber: string) {
  return cardNumber.replace(/^.*(\d{4})$/, "**** **** **** **** $1");
}
export const copyFn = (text: string) => {
  uni.setClipboardData({
    data: text,
    success: function () {
      uni.showToast({
        title: "复制成功",
        icon: "none",
        position: "top",
      });
    },
  });
};
export const checkForUpdates = async () => {
  const appVersion = uni.getSystemInfoSync().appVersion;
  try {
    const res1: any = await selectVersion({ appVer: appVersion });
  } catch (error: any) {
    uni.showModal({
      title: "版本更新提示",
      content: "当前版本不是最新！点击更新更新版本",
      showCancel: false,
      confirmText: "更新",
      success: async function (res) {
        if (res.confirm) {
          plus.runtime.openURL(error.doawLoadUrl);
        } else if (!res.cancel) {
          checkForUpdates();
        }
      },
    });
  }
};
export const aliPayFn = () => {
  console.log("jinlaile");
  let wxChannel: any = "";
  let aliChannel: any = "";
  plus.payment.getChannels(
    (channels) => {
      console.log(channels, "channels");

      for (const i in channels) {
        if (channels[i].id === "wxpay") {
          wxChannel = channels[i];
        } else if (channels[i].id === "alipay") {
          aliChannel = channels[i];
        }
      }
    },
    (e) => {
      console.log(e.message);
    }
  );
  console.log(aliChannel);

  // plus.payment.request(aliChannel,)
};
export function isImageOrVideo(url: string) {
  const imageExtensions = ["jpg", "jpeg", "png", "gif", "bmp", "webp", "svg"];
  const videoExtensions = ["mp4", "mov", "m4v", "avi", "mpg", "webm", "wmv"];

  // 获取链接中的最后一部分，通常是文件名或文件扩展名
  const lastPart: string = url.split("/").pop() as string;

  // 获取文件扩展名
  const extension: string = lastPart.split(".").pop() as string;

  // 判断是否为图片
  if (imageExtensions.includes(extension.toLowerCase())) {
    return "image";
  }

  // 判断是否为视频
  if (videoExtensions.includes(extension.toLowerCase())) {
    return "video";
  }

  // 若不是图片或视频，则返回null或其他自定义值
  return null;
}
